package cn.pzhuweb.controller;

import cn.pzhuweb.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author KK
 * @create 2022/3/3 18:08
 */
@RestController
public class UserController {
    @Autowired
    HttpSession session;

    @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
    public String getById(@PathVariable Integer id) {
        System.out.println("ID" + id);
        return "{'module':'user getById'}";
    }

    @PostMapping("/users")
    public String save( @RequestBody User user) {
        System.out.println("user save..." + user);
        session.setAttribute("user",user);
        return "{'module':'user save'}";
    }

    @DeleteMapping(value = "/users/{id}")
    public String delete(@PathVariable Integer id) {
        User user1 = (User) session.getAttribute("user");
        if (user1.getId().equals(id)){
            System.out.println(user1);
            session.removeAttribute("user");
            return "{'module':'user delete'}";
        } else {
            return "{'module':'user not delete'}";
        }
    }

    @PutMapping(value = "/users")
    public String update(@RequestBody User user) {
        User user1 = (User)session.getAttribute("user");
        if (user1.getId().equals(user.getId())){
            System.out.println(user);
            return "{'module':'user update'}";
        } else {
            return "{'module':'user not update'}";
        }

    }

}
